﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.SharePoint.Explorer;
using SPVSX.Explorer.Extensions.Properties;
using SPVSX.Explorer.Extensions.NodeInformation;
using SPVSX.Explorer.Extensions.CommandIds;

namespace SPVSX.Explorer.Extensions
{
    /// <summary>
    /// Defines a new node type that represents a Theme Gallery on a SharePoint site.
    /// </summary>
    // Enables Visual Studio to discover and load this extension.
    [Export(typeof(IExplorerNodeTypeProvider))]
    // Specifies the ID for this new node type.
    [ExplorerNodeType(ThemeGalleryNodeTypeProvider.ThemeGalleryNodeTypeId)]
    class ThemeGalleryNodeTypeProvider
    {
        #region Constants

        /// <summary>
        /// The node type id
        /// </summary>
        internal const string ThemeGalleryNodeTypeId = "SPVSX.SharePoint.ThemeGallery";

        #endregion

        #region Methods

        /// <summary>
        /// Initialise the node with its icon and event hookups
        /// </summary>
        /// <param name="typeDefinition">The node type defintion</param>
        public void InitializeType(IExplorerNodeTypeDefinition typeDefinition)
        {
            typeDefinition.DefaultIcon = Resources.ThemesNode.ToBitmap();
            typeDefinition.IsAlwaysLeaf = true;

            typeDefinition.NodePropertiesRequested += NodePropertiesRequested;
            typeDefinition.NodeMenuItemsRequested += NodeMenuItemsRequested;
            typeDefinition.NodeChildrenRequested += NodeChildrenRequested;
        }

        /// <summary>
        /// Creates the new theme nodes with the specified icon.
        /// </summary>
        /// <param name="sender">The sender object</param>
        /// <param name="e">The ExplorerNodeEventArgs object</param>
        private void NodeChildrenRequested(object sender, ExplorerNodeEventArgs e)
        {
            
        }


        private void NodeMenuItemsRequested(object sender, ExplorerNodeMenuItemsRequestedEventArgs e)
        {
            e.MenuItems.Add(Resources.ThemeGalleryNode_ViewInBrowser, 5).Click += new EventHandler<MenuItemEventArgs>(ThemeNodeTypeProvider_Click);
        }

        void ThemeNodeTypeProvider_Click(object sender, MenuItemEventArgs e)
        {
        }

        /// <summary>
        /// Retrieves properties that are displayed in the Properties window when
        /// a theme gallery node is selected.
        /// </summary>
        /// <param name="sender">The sender object</param>
        /// <param name="e">The ExplorerNodePropertiesRequestedEventArgs object</param>
        private void NodePropertiesRequested(object sender,
            ExplorerNodePropertiesRequestedEventArgs e)
        {
            var themeGalleryNodeInfo = e.Node.GetAnnotation<ThemeGalleryNodeInfo>();

            // Call the custom SharePoint command to get the Web Part properties.
            Dictionary<string, string> properties =
                e.Node.Context.SharePointConnection.ExecuteCommand<
                ThemeGalleryNodeInfo, Dictionary<string, string>>(
                ThemeGalleryCommandIds.GetThemeGalleryProperties, themeGalleryNodeInfo);

            object propertySource = e.Node.Context.CreatePropertySourceObject(properties);
            e.PropertySources.Add(propertySource);
        }

        /// <summary>
        /// Creates all of the individual theme nodes under the theme gallery node.
        /// </summary>
        /// <param name="parentNode">The parent node</param>
        internal static void GetThemes(IExplorerNode parentNode)
        {
            // Call the custom SharePoint command to get items from the Theme gallery.
            var themes = parentNode.Context.SharePointConnection.ExecuteCommand<ThemeNodeInfo[]>(
                ThemeGalleryCommandIds.GetThemes);

            if (themes != null)
            {
                foreach (ThemeNodeInfo theme in themes)
                {
                    // Create a new annotation object to store the current theme item with the new node.
                    var annotations = new Dictionary<object, object>() 
                    { 
                        { typeof(ThemeNodeInfo), theme } 
                    };

                    // Create the new node for the current Web Part item.
                    parentNode.ChildNodes.Add(ThemeNodeTypeProvider.ThemeNodeTypeId,
                        theme.Name,
                        annotations);
                }
            }
        }

        #endregion
    }
}
